home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicFileChooserUI.java < prev    next >
Text File  |  1998-06-30  |  15KB  |  524 lines

  1. /*
  2.  * @(#)BasicFileChooserUI.java    1.4 98/04/14
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.preview.*;
  25. import com.sun.java.swing.preview.filechooser.*;
  26. import com.sun.java.swing.event.*;
  27. import com.sun.java.swing.plaf.*;
  28. import com.sun.java.swing.plaf.basic.*;
  29. import java.awt.*;
  30. import java.awt.event.*;
  31. import java.beans.*;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.util.*;
  35.  
  36. /**
  37.  * Basic L&F implementation of a FileChooser.  
  38.  *
  39.  * @version %i% %g%
  40.  * @author Jeff Dinkins
  41.  */
  42. public abstract class BasicFileChooserUI extends FileChooserUI implements PropertyChangeListener {
  43.  
  44.     /* FileView icons */
  45.     protected Icon directoryIcon = null;
  46.     protected Icon fileIcon = null;
  47.     protected Icon computerIcon = null;
  48.     protected Icon hardDriveIcon = null;
  49.     protected Icon floppyDriveIcon = null;
  50.  
  51.     protected Icon newFolderIcon = null;
  52.     protected Icon upFolderIcon = null;
  53.     protected Icon homeFolderIcon = null;
  54.     protected Icon listViewIcon = null;
  55.     protected Icon detailsViewIcon = null;
  56.  
  57.     protected String saveButtonText = null;
  58.     protected String openButtonText = null;
  59.     protected String cancelButtonText = null;
  60.     protected String updateButtonText = null;
  61.     protected String helpButtonText = null;
  62.  
  63.     protected static String saveButtonToolTipText = null;
  64.     protected static String openButtonToolTipText = null;
  65.     protected static String cancelButtonToolTipText = null;
  66.     protected static String updateButtonToolTipText = null;
  67.     protected static String helpButtonToolTipText = null;
  68.  
  69.     // Some of the more generic FileChooser functions
  70.     private Action approveSelectionAction = new ApproveSelectionAction();
  71.     private Action cancelSelectionAction = new CancelSelectionAction();
  72.     private Action updateAction = new UpdateAction();
  73.     private Action newFolderAction = new NewFolderAction();
  74.     private Action goHomeAction = new GoHomeAction();
  75.     private Action changeToParentDirectoryAction = new ChangeToParentDirectoryAction();
  76.  
  77.     private JFileChooser filechooser = null;
  78.  
  79.     private AcceptAllFileFilter acceptAllFileFilter = new AcceptAllFileFilter();
  80.  
  81.     /* FileView type descriptions */
  82.     // PENDING(jeff) - I18N - these should be localized
  83.     protected String fileDescription = "Generic File";
  84.     protected String directoryDescription = "Directory";
  85.  
  86.     protected BasicDirectoryModel model = null;
  87.  
  88.     protected BasicFileView fileView = new BasicFileView();
  89.  
  90.     protected JPanel accessoryPanel = null;
  91.  
  92.     /**
  93.      * Abstract methods
  94.      */
  95.     public abstract String getFileName();
  96.     public abstract void setFileName(String filename);
  97.     public abstract String getDirectoryName();
  98.     public abstract void setDirectoryName(String dirname);
  99.     public abstract void ensureFileIsVisible(File f);
  100.     public abstract void rescanCurrentDirectory();
  101.     public abstract void propertyChange(PropertyChangeEvent e);
  102.  
  103.  
  104.     //
  105.     // ComponentUI Interface Implementation methods
  106.     //
  107.  
  108.     /**
  109.      * returns a BasicFileChooserUI
  110.      */
  111.     // public static ComponentUI createUI(JComponent c) {
  112.     //     return new BasicFileChooserUI((JFileChooser)c);
  113.     // }
  114.  
  115.     public BasicFileChooserUI(JFileChooser b) {
  116.     }
  117.  
  118.     public void installUI(JComponent c) {
  119.     accessoryPanel = new JPanel(new BorderLayout());
  120.     filechooser = (JFileChooser) c;
  121.  
  122.     loadIcons();
  123.     loadStrings();
  124.     createModel();
  125.     installComponents();
  126.     addListeners();
  127.     }
  128.  
  129.     public void uninstallUI(JComponent c) {
  130.     filechooser.removeAll();
  131.  
  132.     uninstallListeners();
  133.  
  134.     if(accessoryPanel != null) {
  135.         accessoryPanel.removeAll();
  136.     }
  137.  
  138.     filechooser = null;
  139.     accessoryPanel = null;
  140.     }
  141.  
  142.     protected void uninstallListeners() {
  143.     if(getFileChooser() != null) {
  144.         getFileChooser().removeAll();
  145.         getFileChooser().removePropertyChangeListener(this);
  146.         getFileChooser().removePropertyChangeListener(model);
  147.     }
  148.     }
  149.  
  150.     protected void loadIcons() {
  151.     directoryIcon    = UIManager.getIcon("FileView.directoryIcon");
  152.     fileIcon         = UIManager.getIcon("FileView.fileIcon");
  153.     computerIcon     = UIManager.getIcon("FileView.computerIcon");
  154.     hardDriveIcon    = UIManager.getIcon("FileView.hardDriveIcon");
  155.     floppyDriveIcon  = UIManager.getIcon("FileView.floppyDriveIcon");
  156.  
  157.     newFolderIcon = UIManager.getIcon("FileChooser.newFolderIcon");
  158.     upFolderIcon = UIManager.getIcon("FileChooser.upFolderIcon");
  159.     homeFolderIcon = UIManager.getIcon("FileChooser.homeFolderIcon");
  160.     detailsViewIcon = UIManager.getIcon("FileChooser.detailsViewIcon");
  161.     listViewIcon = UIManager.getIcon("FileChooser.listViewIcon");
  162.  
  163.     }
  164.  
  165.     protected void loadStrings() { 
  166.     saveButtonText = UIManager.getString("FileChooser.saveButtonText");
  167.     openButtonText = UIManager.getString("FileChooser.openButtonText");
  168.     cancelButtonText = UIManager.getString("FileChooser.cancelButtonText");
  169.     updateButtonText = UIManager.getString("FileChooser.updateButtonText");
  170.     helpButtonText = UIManager.getString("FileChooser.helpButtonText");
  171.  
  172.     saveButtonToolTipText = UIManager.getString("FileChooser.saveButtonToolTipText");
  173.     openButtonToolTipText = UIManager.getString("FileChooser.openButtonToolTipText");
  174.     cancelButtonToolTipText = UIManager.getString("FileChooser.cancelButtonToolTipText");
  175.     updateButtonToolTipText = UIManager.getString("FileChooser.updateButtonToolTipText");
  176.     helpButtonToolTipText = UIManager.getString("FileChooser.helpButtonToolTipText");
  177.     }
  178.  
  179.     protected void createModel() {
  180.     model = new BasicDirectoryModel(getFileChooser());
  181.     }
  182.  
  183.     public BasicDirectoryModel getModel() {
  184.     return model;
  185.     }
  186.  
  187.     protected void addListeners() {
  188.     getFileChooser().addPropertyChangeListener(model);
  189.     getFileChooser().addPropertyChangeListener(this);
  190.     }
  191.  
  192.     
  193.     public JFileChooser getFileChooser() {
  194.     return filechooser;
  195.     }
  196.  
  197.     public JPanel getAccessoryPanel() {
  198.     return accessoryPanel;
  199.     }
  200.  
  201.  
  202.     public ListSelectionListener createListSelectionListener() {
  203.     return new SelectionListener();
  204.     }
  205.  
  206.     protected class DoubleClickListener extends MouseAdapter {
  207.     JList list;
  208.     public  DoubleClickListener(JList list) {
  209.         this.list = list;
  210.     }
  211.     
  212.     public void mouseClicked(MouseEvent e) {
  213.         if (e.getClickCount() == 2) {
  214.         int index = list.locationToIndex(e.getPoint());
  215.         if(index >= 0) {
  216.             File f = (File) list.getModel().getElementAt(index);
  217.             if(getFileChooser().isTraversable(f)) {
  218.             list.clearSelection();
  219.             getFileChooser().setCurrentDirectory(f);
  220.             } else {
  221.             getFileChooser().approveSelection();
  222.             }
  223.         }
  224.         }
  225.     }
  226.     }
  227.  
  228.     protected MouseListener createDoubleClickListener(JList list) {
  229.     return new DoubleClickListener(list);
  230.     }
  231.  
  232.     protected class SelectionListener implements ListSelectionListener {
  233.     public void valueChanged(ListSelectionEvent e) {
  234.         if(!e.getValueIsAdjusting()) {
  235.         JList list = (JList) e.getSource();
  236.         File f = (File) list.getSelectedValue();
  237.         if(f != null && ((getFileChooser().isDirectorySelectionEnabled()) ||
  238.                  !getFileChooser().isTraversable(f))) {
  239.             getFileChooser().setSelectedFile(f);
  240.         }
  241.         }
  242.     }
  243.     }
  244.     
  245.  
  246.     /**
  247.      * Returns the default accept all file filter
  248.      */
  249.     public FileFilter getAcceptAllFileFilter() {
  250.     return acceptAllFileFilter;
  251.     }
  252.  
  253.  
  254.     public FileView getFileView() {
  255.     return fileView;
  256.     }
  257.  
  258.  
  259.     /**
  260.      * Returns the title of this dialog
  261.      */
  262.     public String getDialogTitle() {
  263.     return getApproveButtonText();
  264.     }
  265.  
  266.     public abstract void installComponents();
  267.  
  268.     public String getApproveButtonText() {
  269.     String buttonText = getFileChooser().getApproveButtonText();
  270.     if(buttonText != null) {
  271.         return buttonText;
  272.     }
  273.  
  274.     if(getFileChooser().getDialogType() == JFileChooser.OPEN_DIALOG) {
  275.         return openButtonText;
  276.     } else if(getFileChooser().getDialogType() == JFileChooser.SAVE_DIALOG) {
  277.         return saveButtonText;
  278.     } 
  279.     return null;
  280.     }
  281.  
  282.     public String getApproveButtonToolTipText() {
  283.     String tooltipText = getFileChooser().getApproveButtonToolTipText();
  284.     if(tooltipText != null) {
  285.         return tooltipText;
  286.     }
  287.  
  288.     if(getFileChooser().getDialogType() == JFileChooser.OPEN_DIALOG) {
  289.         return openButtonToolTipText;
  290.     } else if(getFileChooser().getDialogType() == JFileChooser.SAVE_DIALOG) {
  291.         return saveButtonToolTipText;
  292.     } 
  293.     return null;
  294.     }
  295.  
  296.     public Action getNewFolderAction() {
  297.     return newFolderAction;
  298.     }
  299.  
  300.     public Action getGoHomeAction() {
  301.     return goHomeAction;
  302.     }
  303.  
  304.     public Action getChangeToParentDirectoryAction() {
  305.     return changeToParentDirectoryAction;
  306.     }
  307.  
  308.     public Action getApproveSelectionAction() {
  309.     return approveSelectionAction;
  310.     }
  311.  
  312.     public Action getCancelSelectionAction() {
  313.     return cancelSelectionAction;
  314.     }
  315.  
  316.     public Action getUpdateAction() {
  317.     return updateAction;
  318.     }
  319.  
  320.  
  321.     /**
  322.      * Creates a new folder.
  323.      */
  324.     protected class NewFolderAction extends AbstractAction {
  325.     protected NewFolderAction() {
  326.         super("New Folder");
  327.     }
  328.     public void actionPerformed(ActionEvent e) {
  329.         File currentDirectory = getFileChooser().getCurrentDirectory();
  330.         File newFolder = null;
  331.         try {
  332.         newFolder = getFileChooser().getFileSystemView().createNewFolder(currentDirectory);
  333.         } catch (IOException exc) {
  334.         JOptionPane.showMessageDialog(getFileChooser(), "Error creating new folder: " + exc,
  335.                           "Error Creating New Folder", JOptionPane.ERROR_MESSAGE);
  336.         return;
  337.         } 
  338.         getFileChooser().rescanCurrentDirectory();
  339.         getFileChooser().ensureFileIsVisible(newFolder);
  340.     }
  341.     }
  342.  
  343.     /**
  344.      * Acts on the "home" key event or equivalent event.
  345.      */
  346.     protected class GoHomeAction extends AbstractAction {
  347.     protected GoHomeAction() {
  348.         super("Go Home");
  349.     }
  350.     public void actionPerformed(ActionEvent e) {
  351.         getFileChooser().setCurrentDirectory(null);
  352.     }
  353.     }
  354.  
  355.     protected class ChangeToParentDirectoryAction extends AbstractAction {
  356.     protected ChangeToParentDirectoryAction() {
  357.         super("Go Up");
  358.     }
  359.     public void actionPerformed(ActionEvent e) {
  360.         getFileChooser().changeToParentDirectory();
  361.     }
  362.     }
  363.  
  364.     /**
  365.      * Responds to an Open or Save request
  366.      */
  367.     protected class ApproveSelectionAction extends AbstractAction {
  368.     public void actionPerformed(ActionEvent e) {
  369.         String filename = getFileName();
  370.         FileSystemView fs = getFileChooser().getFileSystemView();
  371.         File dir = getFileChooser().getCurrentDirectory();
  372.  
  373.         if(filename != null) {
  374.         // Remove whitespace from beginning and end of filename
  375.         filename = filename.trim();
  376.         }
  377.         if(filename != null && !filename.equals("")) {
  378.         // check for directory change action
  379.         File selectedFile = fs.createFileObject(filename);
  380.         if(!selectedFile.isAbsolute()) {
  381.            selectedFile = fs.createFileObject(dir, filename);
  382.         }
  383.         if(selectedFile.isDirectory() && getFileChooser().isTraversable(selectedFile)
  384.                                       && !getFileChooser().isDirectorySelectionEnabled())
  385.         {
  386.             getFileChooser().setCurrentDirectory(selectedFile);
  387.             return;
  388.         } else if ((!selectedFile.isDirectory() && getFileChooser().isFileSelectionEnabled()) || 
  389.                (selectedFile.isDirectory() && getFileChooser().isDirectorySelectionEnabled())) {
  390.             getFileChooser().setSelectedFile(selectedFile);
  391.         }
  392.         } 
  393.         getFileChooser().approveSelection();
  394.     }
  395.     }
  396.  
  397.  
  398.     /**
  399.      * Responds to a cancel request.
  400.      */
  401.     protected class CancelSelectionAction extends AbstractAction {
  402.     public void actionPerformed(ActionEvent e) {
  403.         getFileChooser().cancelSelection();
  404.     }
  405.     } 
  406.  
  407.     /**
  408.      * Rescans the files in the current directory
  409.      */
  410.     protected class UpdateAction extends AbstractAction {
  411.     public void actionPerformed(ActionEvent e) {
  412.         getFileChooser().setCurrentDirectory(
  413.         getFileChooser().getFileSystemView().createFileObject(getDirectoryName())
  414.         );
  415.         getFileChooser().rescanCurrentDirectory();
  416.     }
  417.     }
  418.  
  419.     
  420.     // *********************************************
  421.     // ***** the default AcceptAll file filter *****
  422.     // *********************************************
  423.     protected class AcceptAllFileFilter extends FileFilter {
  424.     
  425.     public AcceptAllFileFilter() {
  426.     }
  427.     
  428.     public boolean accept(File f) {
  429.         return true;
  430.     }
  431.     
  432.     public String getDescription() {
  433.         return UIManager.getString("FileChooser.acceptAllFileFilterText");
  434.     }
  435.     }
  436.  
  437.  
  438.     // ***********************
  439.     // * FileView operations *
  440.     // ***********************
  441.     protected class BasicFileView extends FileView {
  442.     // PENDING(jeff) - pass in the icon cache size
  443.     protected Hashtable iconCache = new Hashtable();
  444.     
  445.     public BasicFileView() {
  446.     }
  447.     
  448.     public void clearIconCache() {
  449.         iconCache = new Hashtable();
  450.     }
  451.     
  452.     public String getName(File f) {
  453.         String fileName = null;
  454.         if(f != null) {
  455.         fileName = f.getName();
  456.         if (fileName.equals("")) {
  457.             fileName = f.getPath();
  458.         }
  459.         }
  460.         return fileName;
  461.     }
  462.     
  463.     
  464.     public String getDescription(File f) {
  465.         return f.getName();
  466.     }
  467.     
  468.     public String getTypeDescription(File f) {
  469.         if (f.isDirectory()) {
  470.         return directoryDescription;
  471.         } else {
  472.         return fileDescription;
  473.         }
  474.     }
  475.     
  476.     public Icon getCachedIcon(File f) {
  477.         return (Icon) iconCache.get(f);
  478.     }
  479.     
  480.     public void cacheIcon(File f, Icon i) {
  481.         if(f == null || i == null) {
  482.         return;
  483.         }
  484.         iconCache.put(f, i);
  485.     }
  486.     
  487.     public Icon getIcon(File f) {
  488.         Icon icon = getCachedIcon(f);
  489.         if(icon != null) {
  490.         return icon;
  491.         }
  492.         if (f != null && f.isDirectory()) {
  493.         if(getFileChooser().getFileSystemView().isRoot(f)) {
  494.             icon = hardDriveIcon;
  495.         } else {
  496.             icon = directoryIcon;
  497.         }
  498.         } else {
  499.         icon = fileIcon;
  500.         }
  501.         cacheIcon(f, icon);
  502.         return icon;
  503.     }
  504.     
  505.     public Boolean isTraversable(File f) {
  506.         if (f.isDirectory()) {
  507.         return Boolean.TRUE;
  508.         } else {
  509.         return Boolean.FALSE;
  510.         }
  511.     }
  512.     
  513.     public Boolean isHidden(File f) {
  514.         String name = f.getName();
  515.         if(name != null && name.charAt(0) == '.') {
  516.         return Boolean.TRUE;
  517.         } else {
  518.         return Boolean.FALSE;
  519.         }
  520.     }
  521.     }
  522.  
  523. }
  524.